﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class FormMain : Form
    {
        public ForumService forumService;
        List<string> items = new List<string>();
        int lvl;
        List<string> updateListSource = new List<string>();
        List<string> dataList;

        public FormMain()
        {
            InitializeComponent();
            panel1.Show();
            MainForumPanel.Hide();
            forumService = new ForumService(this);
            //MainForumPanel.BackgroundImage = Image.FromFile("~/pic.jpeg");
            this.updateListSource.Add("Welcome");
            this.textBox1.Enabled = false;
            this.updateList.DataSource = updateListSource;
            this.listBox1.HorizontalScrollbar = true;
            this.listBox1.MouseDoubleClick += new MouseEventHandler(listBox1_MouseDoubleClick);
        }


        //loads-------------------------------------

        public void loadUserName(string name)
        {
            this.textBox1.Text = name;
        }


        public void loadMainForum(List<string> subs, string header)//loads the main forum panel with subforum list
        {
            this.dataList = subs;
            panel1.Hide();
            MainForumPanel.Show();
            listBox1.DataSource = null;
            listBox1.DataSource = subs;
            titleLabel.Text = header;
            this.button4.Enabled = true;
            this.lvl = 1;
        }

        public void loadSubForum(List<string> subs, string header)//loads the main forum panel with subforum
        {
            this.dataList = subs;
            listBox1.DataSource = null;
            listBox1.DataSource = subs;
            titleLabel.Text = header;
            this.lvl = 2;
        }

        public void loadThread(List<string> subs, string header)//loads the main forum panel with thread 
        {
            this.dataList = subs;
            listBox1.DataSource = null;
            listBox1.DataSource = subs;
            titleLabel.Text = header;
            this.button4.Enabled = false;
            this.lvl = 3;
        }

        //buttons-------------------------------------


        private void button1_Click(object sender, EventArgs e)//login
        {
            new FormLogin(this).Show();
        }

        private void button2_Click(object sender, EventArgs e)//guest entry
        {
            forumService.login("guest", "", "");
            this.buttonManager.Hide();
            this.buttonCreat.Hide();
            this.buttonDelete.Hide();
            this.buttonEdit.Hide();
            this.buttonUList.Hide();

        }

        private void FormMain_Load(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)//return to main forum
        {
            forumService.getMainForum();
        }

        private void button4_Click(object sender, EventArgs e)//Enter 
        {
            int r = listBox1.SelectedIndex;
            switch (this.lvl)
            {
                case 1:
                    this.forumService.getSubForum(r);
                    break;
                case 2:
                    this.forumService.getThread(r);
                    break;
            }
        }







        public void updateUpdatePanel(string item)
        {
            this.updateList.DataSource = null;
            this.updateListSource.Insert(0,item);
            this.updateList.DataSource = this.updateListSource;
           // this.updateList.Items.Add(item);
        }

        private void MainForumPanel_Paint(object sender, PaintEventArgs e)
        {

        }

        private void buttonUList_Click(object sender, EventArgs e)//user list
        {
            new FormUserList(this).Show();
        }

        private void buttonLogout_Click(object sender, EventArgs e)//logout
        {
            this.Dispose();
        }

        private void buttonDelete_Click(object sender, EventArgs e)//delete
        {
            int r = listBox1.SelectedIndex;
            switch (this.lvl)
            {
                case 1:
                    this.forumService.deleteSubForum(r);
                    break;
                case 2:
                    this.forumService.deleteThread(r);
                    break;
                case 3:
                    if (r == 0)
                        new FormError("You can't delete the first post").Show();
                    else
                        this.forumService.deletePost(r);
                    break;
            }
        }


        private void buttonEdit_Click(object sender, EventArgs e)//Edit
        {
            /*
            int r = listBox1.SelectedIndex;
            switch (this.lvl)
            {
                case 2:
                    new FormEdit(1, this, r).Show();
                    break;
                case 3:
                    new FormEdit(2, this, r).Show();
                    break;
            }
            */
        }

        private void buttonManager_Click(object sender, EventArgs e)//manager access
        {
            this.forumService.getForumStats();
        }

        private void buttonEdit_Click_1(object sender, EventArgs e)//Edit
        {
            
            int r = listBox1.SelectedIndex;
            switch (this.lvl)
            {
                case 2:
                    new FormEdit(this, 1, r).Show();
                    break;
                case 3:
                    if (r == 0)
                        new FormError("You can't edit the first post").Show();
                    else
                        new FormEdit(this ,2 , r).Show();
                    break;
            }
            
        }

        private void buttonCreat_Click(object sender, EventArgs e)//create
        {
            int r = listBox1.SelectedIndex;
            switch (this.lvl)
            {
                case 1:
                    new FormNewSub(this).Show();
                    break;
                case 2:
                    new FormNewThread(this).Show();
                    break;
                case 3:
                    new FormPostReply(this).Show();
                    break;
            }

        }

        private void buttonDelete_Click_1(object sender, EventArgs e)//delete
        {//fix to post
            int r = listBox1.SelectedIndex;
            switch (this.lvl)
            {
                case 1:
                    this.forumService.deleteSubForum(r);
                    break;
                case 2:
                    this.forumService.deleteThread(r);
                    break;
                case 3:
                    if (r == 0)
                        new FormError("You can't delete the first post").Show();
                    else
                        this.forumService.deletePost(r);
                    break;
            }
        }

        private void buttonUList_Click_1(object sender, EventArgs e)
        {
            new FormUserList(this).Show();
        }

        private void buttonLogout_Click_1(object sender, EventArgs e)
        {
            this.forumService.logout();
            this.Dispose();
        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            new FormRegister(this.forumService).Show();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (!this.checkBox1.Checked)
                this.updateList.DataSource = null;
            else
                this.updateList.DataSource = this.updateListSource;
        }

        private void listBox1_MouseDoubleClick(object sender, EventArgs e)
        {

            new FormInfo(this.dataList.ElementAt(this.listBox1.SelectedIndex)).Show();
        }



        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            this.forumService.logout();
            base.OnFormClosing(e);
        }

        
    }
}
